package leeon.forpeddy;

import ipworks.IPWorksException;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.TooManyListenersException;

import leeon.forpeddy.download.HtmlDownloader;
import leeon.forpeddy.excel.GradeExcelWrapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MultiThreadGradeHelper {
	
	
	static Log logger = LogFactory.getLog(MultiThreadGradeHelper.class);
	
	public static final String BASE_DIR = "D:\\project\\eurospace\\forpeddy\\misc\\";
	public static final String HTTP_URL_5 = "http://218.1.69.105/bachelor/student/scorecollecttablebk_ChongOnly.jsp?studentno=";//5开头的学生
	public static final String HTTP_URL_6 = "http://218.1.69.105/acad/student/scorecollecttablebk_ygao.jsp?studentno=";//6开头和字母开头
	public static final String HTTP_URL_70 = "http://218.1.69.105/acad/student/scorecollecttablebk3_ygao.jsp?studentno=";//7开头，专升本，倒数第四位0
	public static final String HTTP_URL_72 = "http://218.1.69.105/gsz/student/scorecollecttablebk.jsp?studentno=";//7开头，高起专的url，倒数第四位2
	public static final String HTTP_URL_71 = "http://218.1.69.105/gqb/student/scorecollecttablebk.jsp?studentno=";//7开头，高起本的学生，倒数第四位1
	
	
	public static final int MAX_THREAD_SIZE = 4;
	
	private List<String> studentList = null;
	private int current = 0;
	GradeExcelWrapper list = null;
	
	private MultiThreadGradeHelper() {
		try {
			this.list = new GradeExcelWrapper(new File(BASE_DIR + "list.xls"));
			this.studentList = list.readStudentNo();
			this.list.save();
		} catch (Exception e) {
			logger.error("init wrong ...", e);
		}
	}
	
	public synchronized String getNextStudentNo() {
		if (current < studentList.size()) {
			logger.info("download progress info:[all("+studentList.size()+"), current("+(current+1)+")]");
			return studentList.get(current++);
		} else
			return null;
	}
			
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MultiThreadGradeHelper gh = new MultiThreadGradeHelper();
		
		Runner[] rs = new Runner[MAX_THREAD_SIZE];
		for (Runner r : rs) {
			r = new Runner(gh);
			Thread t = new Thread(r);
			t.start();
		}
		
		
	}

}


class Runner implements Runnable {
	
	MultiThreadGradeHelper gh;
	
	public Runner(MultiThreadGradeHelper gh) {
		this.gh = gh;
	}
	
	public void run() {
		try {
			doStartDownload();
		} catch (Exception e) {
			MultiThreadGradeHelper.logger.info("download wrong ...", e);
		}
	}
	
	public void doStartDownload() throws IPWorksException, TooManyListenersException, IOException {
		HtmlDownloader h = new HtmlDownloader();
		String studentNo;
		while ((studentNo = gh.getNextStudentNo()) != null) {
			File file = new File(MultiThreadGradeHelper.BASE_DIR + studentNo + ".html");
			if (!file.exists() || file.length() == 0) {
				//判断studentNo
				String url = null;
				//5开头的学生
				if (studentNo.startsWith("5")) {
					url = MultiThreadGradeHelper.HTTP_URL_5;
				//6开头的学生
				} else if (studentNo.startsWith("6")) {
					url = MultiThreadGradeHelper.HTTP_URL_6;
				//7开头的学生
				} else if (studentNo.startsWith("7")) {
					if (studentNo.charAt(8) == '0') {
						url = MultiThreadGradeHelper.HTTP_URL_70;
					} else if (studentNo.charAt(8) == '1') {
						url = MultiThreadGradeHelper.HTTP_URL_71;
					} else if (studentNo.charAt(8) == '2') {
						url = MultiThreadGradeHelper.HTTP_URL_72;
					} else {
						MultiThreadGradeHelper.logger.warn("there is a student " + studentNo + " can not judge his subject");
						continue;
					}
				//字母开头的学生
				} else {
					url = MultiThreadGradeHelper.HTTP_URL_6;
				}
				
				h.start(url + studentNo);
				h.saveToFile(file);

			} else {
				MultiThreadGradeHelper.logger.info("download html for student " + studentNo + " file exists");
			}
		}
		h.destroy();
	}

	
}
